import axios from "axios";
import { Message, MessageBox } from "element-ui";

const instance = axios.create({
  // baseURL: process.env.VUE_APP_API,
  
  baseURL: 'http://shiyansong.cn:8888/api/private/v1/',
  timeout: 1000,
  // headers: {'X-Custom-Header': 'foobar'}
});

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  let token = localStorage.getItem('token');
    if(token){
      config.headers.Authorization = token
    }
    return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
  // 对响应数据做点什么

  // let {data:{meta:{msg,status}}}=response
  // if(msg=="无效token")
  let { msg, status } = response.data.meta
     if (msg == '无效token') {
   MessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
   })
    .then(() => {
   location.href ='#/login'
    })
    .catch(() => {
   return false
    });
     }
     if(status ==400 || status ==401 || status ==403 || status == 404){
   Message.error(msg);
     }
     if(status ==200 || status ==201){
   Message.success(msg)
    }
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

export default instance